package com.woniuxy.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.shop.entity.Cart;
import com.woniuxy.shop.entity.Order;
import com.woniuxy.shop.entity.Orderitem;
import com.woniuxy.shop.entity.Product;
import com.woniuxy.shop.mapper.CartMapper;
import com.woniuxy.shop.mapper.OrderMapper;
import com.woniuxy.shop.mapper.OrderitemMapper;
import com.woniuxy.shop.mapper.ProductMapper;
import com.woniuxy.shop.service.CartService;
import com.woniuxy.shop.service.OrderService;
import com.woniuxy.shop.service.ProductService;
import com.woniuxy.shop.vo.CartItemVo;
import com.woniuxy.shop.vo.OrderPreViewVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.function.Consumer;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fanqiwen
 * @since 2023-02-07 11:56:02
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Autowired
    CartMapper cartMapper;
    @Autowired
    CartService cartService;
    @Autowired
    ProductMapper productMapper;
    @Autowired
    OrderitemMapper orderitemMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    ProductService productService;
    @Override
    public OrderPreViewVo queryOrderPreViewVo(Integer userId, Integer[] productIds) {
        //查询出user_id 为userId 并且购物车里有productIds这些商品的信息
        QueryWrapper<Cart> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userId).and(new Consumer<QueryWrapper<Cart>>() {
            @Override
            public void accept(QueryWrapper<Cart> cartQueryWrapper) {
                cartQueryWrapper.in("product_id",productIds);
            }
        });
        List<Cart> carts = cartMapper.selectList(queryWrapper);
        List<CartItemVo>  cartItemVoList =new ArrayList<>();
        //将cart对象 封装为CartItemVo 对象
        OrderPreViewVo orderPreViewVo=new OrderPreViewVo();
        //计算预选订单所有商品的总价
        BigDecimal totalPrice=new BigDecimal("0.00");
        for (Cart cart : carts) {
            //根据cart的商品id查询出对应的商品信息
            Product product = productMapper.selectById(cart.getProductId());
            //生成一个CartItemVo对象存储cart和product的信息
            CartItemVo cartItemVo = new CartItemVo();
            cartItemVo.setId(cart.getId());
            cartItemVo.setProductId(product.getId());
            cartItemVo.setBuycount(cart.getBuycount());
            cartItemVo.setSumprice(cart.getSumprice());
            cartItemVo.setUserId(cart.getUserId());
            cartItemVo.setProductprice(product.getProductprice());
            cartItemVo.setProductimgsrc(product.getProductimgsrc());
            cartItemVo.setProductname(product.getProductname());
            cartItemVoList.add(cartItemVo);
            //计算总价
            totalPrice=totalPrice.add(cart.getSumprice());
        }
        orderPreViewVo.setTotalPrice(totalPrice);
        orderPreViewVo.setCartItemVoList(cartItemVoList);
        //返回预选订单对象
        return orderPreViewVo;
    }

    @Override
    public void addOrder(Integer[] productIds, Integer userId,Integer selectAddressId) {
        //查询预览订单商品信息
        OrderPreViewVo orderPreViewVo = queryOrderPreViewVo(userId, productIds);
        //生成主订单
        Order order=new Order();
        order.setAddressId(selectAddressId);
        order.setOrdernum("No."+new Date().getTime());
        order.setStatus(1);////状态(1未支付,2已支付,3已取消)
        order.setTotalprice(orderPreViewVo.getTotalPrice());
        order.setUserId(userId);
        orderMapper.insert(order);
        //生成订单分项详情
        List<CartItemVo> cartItemVoList = orderPreViewVo.getCartItemVoList();
        for (CartItemVo cartItemVo : cartItemVoList) {
            Orderitem orderitem = new Orderitem();
            orderitem.setProductId(cartItemVo.getProductId());
            orderitem.setProductname(cartItemVo.getProductname());
            orderitem.setProductprice(cartItemVo.getProductprice());
            orderitem.setProductimgsrc(cartItemVo.getProductimgsrc());
            orderitem.setBuycount(cartItemVo.getBuycount());
            orderitem.setSumprice(cartItemVo.getSumprice());
            orderitem.setOrderId(order.getId());
            //写入数据库
            orderitemMapper.insert(orderitem);
            //删除购物车中对应的信息
            cartService.updateBuyCount(cartItemVo.getProductId(),0,userId);
        }

    }
}
